from flask import Blueprint,request,jsonify
from db import db
import json


bp_cate = Blueprint('cate',__name__)

@bp_cate.route('/cateM/',methods=['POST',"GET"])
def cateM():
        if request.method == "POST":
            #获取用户输入name
            data = request.data
            if data:
                data = json.loads(data)
            else:
                data = request.form
            #构造sql评句
            sql = "insert into cate(name) values('%s')"%(data['name'])
            #pymysql操作
            db.insert(sql)
            db.close()
            #返回结果
            return jsonify({'code':200})
        else:
            sql="select * from cate"
            print(sql)
            res = db.get_all(sql)
            return jsonify({'code':200,'list':res})